<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('软件列表')" />
</head>
<body class="gray-bg">
     <div class="container-div">
        <div class="row">
            <div class="col-sm-12 search-collapse">
                <form id="formId">
                    <div class="select-list">
                        <ul>
                            <li>
                                <label>项目类型：</label>
                                <select name="projectType" th:with="type=${@dict.getType('lh_project_type')}">
                                    <option value="">所有</option>
                                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                                </select>
                            </li>
                            <li>
                                <label>软件版本：</label>
                                <select name="softwareVersion">
                                    <option value="">所有</option>
                                    <option th:each="version : ${softwareVersionList}" th:text="${version.softwareVersion}" th:value="${version.softwareVersion}"></option>
                                </select>
                            </li>
                            <li>
                                <label>设备类型：</label>
                                <select name="softwareModel" th:with="type=${@dict.getType('lh_machine_type')}">
                                    <option value="">所有</option>
                                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                                </select>
                            </li>
                            <li>
                                <label>硬件版本：</label>
                                <input type="text" name="supportHardwareVersion"/>
                            </li>
                            <li>
                                <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                                <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                            </li>
                        </ul>
                    </div>
                </form>
            </div>

            <div class="btn-group-sm" id="toolbar" role="group">
                <a class="btn btn-success" onclick="$.operate.add()" shiro:hasPermission="lh:software:add">
                    <i class="fa fa-plus"></i> 新增软件更新
                </a>
                <a class="btn btn-primary single" onclick="syncSoftware()">
<!--                <a class="btn btn-primary single" th:if="${settings != null && settings.type.equals('0')}" onclick="syncSoftware()">-->
                    <i class="fa fa-edit"></i> 同步测试项目
                </a>
            </div>
            <div class="col-sm-12 select-table table-striped">
                <table id="bootstrap-table"></table>
            </div>
        </div>
    </div>
    <th:block th:include="include :: footer" />
    <script th:inline="javascript">
        var editFlag = [[${@permission.hasPermi('lh:software:edit')}]];
        var removeFlag = [[${@permission.hasPermi('lh:software:remove')}]];
        var statusDatas = [[${@dict.getType('lh_software_status')}]];
        var machineTypeDatas = [[${@dict.getType('lh_machine_type')}]];
        var softwareStrategy = [[${@dict.getType('lh_software_strategy')}]];
        var syncStatusDatas = [[${@dict.getType('lh_software_sync_status')}]];
        var prefix = ctx + "lh/software";
        var settings = [[${settings}]];

        $(function() {
            var options = {
                url: prefix + "/list",
                createUrl: prefix + "/add",
                updateUrl: prefix + "/edit/{id}",
                removeUrl: prefix + "/remove",
                exportUrl: prefix + "/export",
                modalName: "软件",
                columns: [{
                    checkbox: true
                },
                {
                    field: 'id',
                    title: '',
                    visible: false
                },
                {
                    field: 'softwareModel',
                    title: '软件/设备类型',
                    formatter: function(value, row, index) {
                        return $.table.selectDictLabel(machineTypeDatas, value);
                    }
                },
                {
                    field: 'machineModel',
                    title: '软件/设备型号'
                },
                {
                    field: 'softwareName',
                    title: '软件名称'
                },
                {
                    field: 'softwareModel',
                    title: '软件类型',
                    formatter: function(value, row, index) {
                        return $.table.selectDictLabel(machineTypeDatas, value);
                    }
                },
                {
                    field: 'softwareVersion',
                    title: '软件版本'
                },
                {
                    title: '更新连接',
                    field: 'updateFile',
                    formatter: function(value, row, index) {
                        var a=document.createElement("a");
                        a.setAttribute("href","/lh/software/download/" + row.id);
                        a.setAttribute("download","");
                        a.innerHTML = value;
                        return a.outerHTML;
                    },
                    cellStyle: formatTableUnit
                },
                {
                    field: 'supportVersion',
                    title: '支持升级版本'
                },
                {
                    field: 'supportHardwareVersion',
                    title: '支持硬件版本'
                },
                {
                    field: 'updateStrategy',
                    title: '更新策略',
                    formatter: function(value, row, index) {
                        return $.table.selectDictLabel(softwareStrategy, value);
                    }
                },
                {
                    field: 'status',
                    title: '状态',
                    formatter: function(value, row, index) {
                        return $.table.selectDictLabel(statusDatas, value);
                    }
                },
                {
                    field: 'syncStatus',
                    title: '同步状态',
                    formatter: function(value, row, index) {
                        return $.table.selectDictLabel(syncStatusDatas, value);
                    }
                },
                {
                    field: 'createTime',
                    title: '创建时间'
                },
                {
                    field: 'downloadNum',
                    title: '下载'
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];
                        if(row.syncStatus == '1') {
                            //当前记录正式环境已经同步, 不允许修改删除
                        } else {
                            actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.id + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                            actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.id + '\')"><i class="fa fa-remove"></i>删除</a>');
                        }
                        if(settings != null && settings.type == '1' && row.syncStatus == '1' && row.testId != null) {
                            actions.push('<a class="btn btn-warning btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="backTest(\'' + row.id + '\')" ><i class="fa fa-refresh"></i>回退到测试</a> ');
                        }
                        return actions.join('');
                    }
                }]
            };
            $.table.init(options);
        });

        //表格超出宽度鼠标悬停显示td内容
        function paramsMatter(value,row,index) {
            var span=document.createElement("span");
            span.setAttribute("title",value);
            span.innerHTML = value;
            return span.outerHTML;
        }
        //td宽度以及内容超过宽度隐藏
        function formatTableUnit(value, row, index) {
            return {
                css: {
                    "white-space": "nowrap",
                    "text-overflow": "ellipsis",
                    "overflow": "hidden",
                    "max-width":"150px"
                }
            }
        }

        function syncSoftware() {
            table.set();
            var rows = $.common.isEmpty(table.options.uniqueId) ? $.table.selectFirstColumns() : $.table.selectColumns(table.options.uniqueId);
            if (rows.length == 0) {
                $.modal.alertWarning("请至少选择一条记录");
                return;
            }
            $.modal.confirm("确认要同步选中的" + rows.length + "条数据吗?", function() {
                $.ajax({
                    url: prefix + "/sync",
                    method: "post",
                    data: { "ids": rows.join() },
                    success: function (data){
                        console.log(data);
                        $.operate.ajaxSuccess(data);
                    }
                })
            });
        }

        function backTest(id) {
            $.ajax({
                url: prefix + "/back",
                method: "post",
                data: { "id": id },
                success: function (data){
                    console.log(data);
                    $.operate.ajaxSuccess(data);
                }
            })
        }
    </script>
</body>
</html>
